package com.jrx.anytxn.transaction.job.delaysms.config;

import com.jrx.anytxn.customer.entity.TlDelaySms;
import com.jrx.anytxn.ribbon.config.GrayVersionConfig;
import com.jrx.anytxn.transaction.config.AnytxnBatchProperties;
import com.jrx.anytxn.transaction.constant.TransBizConstant;
import com.jrx.anytxn.transaction.job.delaysms.step.DelaySmsSendProcessor;
import com.jrx.anytxn.transaction.job.delaysms.step.DelaySmsSendWriter;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.batch.MyBatisCursorItemReader;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.item.support.SynchronizedItemStreamReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
import org.springframework.transaction.PlatformTransactionManager;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 延迟短信
 * @author hcx
 */
@Configuration
@EnableConfigurationProperties(AnytxnBatchProperties.class)
public class DelaySmsConfiguration {
    @Resource(name = "bizSqlSessionFactory")
    SqlSessionFactory sqlSessionFactory;

    @Autowired
    public JobBuilderFactory jobBuilderFactory;

    @Autowired
    public StepBuilderFactory stepBuilderFactory;

    @Autowired
    public GrayVersionConfig grayVersionConfig;

    @Autowired
    private PlatformTransactionManager platformTransactionManager;

    @Autowired
    private AnytxnBatchProperties anytxnBatchProperties;

    @Autowired
    private DelaySmsSendProcessor delaySmsProcessor;

    @Autowired
    private DelaySmsSendWriter delaySmeWriter;

    @Bean
    public Job delaySmsSendJob() {
        return jobBuilderFactory.get("delaySmsSendJob")
                .start(delaySmsSendStep())
                .build();
    }

    @Bean
    public Step delaySmsSendStep() {
        return stepBuilderFactory.get("delaySmsSendStep")
                .<TlDelaySms,TlDelaySms>chunk(anytxnBatchProperties.getDelaySmsSendConfig().getChunkLimit())//表示一批处理100条记录，数据是逐条发送给processor处理的，处理100条记录后，统一返回给writer进行保存处理
                .reader(delaySmsReader())
                .processor(delaySmsProcessor)
                .writer(delaySmeWriter)
                .transactionManager(platformTransactionManager)
                .taskExecutor(new SimpleAsyncTaskExecutor())
                .throttleLimit(anytxnBatchProperties.getDelaySmsSendConfig().getThrottleLimit())//多线程
                .build();
    }

    @Bean
    @StepScope
    public SynchronizedItemStreamReader<TlDelaySms> delaySmsReader() {
        MyBatisCursorItemReader<TlDelaySms> delaySmsReader = new MyBatisCursorItemReader<>();
        delaySmsReader.setSaveState(false);
        delaySmsReader.setSqlSessionFactory(sqlSessionFactory);
        delaySmsReader.setQueryId("com.jrx.anytxn.customer.mapper.ext.ExtTlDelaySmsMapper.selectByStatus");
        SynchronizedItemStreamReader<TlDelaySms> synchronizedItemStreamReader = new SynchronizedItemStreamReader<>();
        Map<String, Object> map = new HashMap<>();
        map.put("dealStatus", TransBizConstant.SMS_SEND_STATUS_D);
        delaySmsReader.setParameterValues(map);
        synchronizedItemStreamReader.setDelegate(delaySmsReader);
        return synchronizedItemStreamReader;
    }
}
